package imooc.synchronize;

/**
 * 对象锁示例2：方法锁形式
 */
public class _03_SynchronizedObjectMethod implements Runnable {
    static _03_SynchronizedObjectMethod instance = new _03_SynchronizedObjectMethod();
//    static _03_SynchronizedObjectMethod instance1 = new _03_SynchronizedObjectMethod();

    @Override
    public void run() {
        method();
    }

    public synchronized void method() {
        System.out.println("我是对象锁的方法修饰符，我叫" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instance);
//        Thread t2 = new Thread(instance1);
        Thread t2 = new Thread(instance);

        t1.start();
        t2.start();

        while (t1.isAlive() || t2.isAlive()){}

        System.out.println("Finished");
    }
}
